package activity.projeto;



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnConnectionParamBean;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

import dados.Personagem;
import dados.Usuario;

public class DataBaseHttpConnection {
	
	public static final int HTTP_TIMEOUT = 30 * 1000;
	private static HttpClient httpClient;	
	private static HttpClient getHttpClient() {
	         if (httpClient == null) {
	             httpClient = new DefaultHttpClient();
	             final HttpParams httpParams = httpClient.getParams();
	             HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_TIMEOUT);
	             HttpConnectionParams.setSoTimeout(httpParams, HTTP_TIMEOUT);
	             ConnManagerParams.setTimeout(httpParams, HTTP_TIMEOUT);
	             
	         }
	         return httpClient;
	      }
	
	public static String executeHttpPost(String url, ArrayList<NameValuePair> paramPost) throws Exception{
		BufferedReader buffer = null;
		try {
			Log.i("Acess", " Entrou no try");
			HttpClient Client = getHttpClient();
			Log.i("Acess", " client criado");
			HttpPost httpPost = new HttpPost();
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramPost);
			httpPost.setEntity(formEntity);
			Log.i("Acess", " resposta Postada");
			HttpResponse httpResponse = Client.execute(httpPost);
			buffer =  new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
			StringBuffer stringBuffer = new StringBuffer("");
			Log.i("Acess", " Buffer criado");
			String line = "";
			String LS = System.getProperty("line.separator");
			Log.i("Acess", " ainda estou no try");
			 while((line = buffer.readLine()) != null){
				stringBuffer.append(line + LS);
				Log.i("Acess", " line = " +line);
			}
			buffer.close();
			// Response
			String response = stringBuffer.toString();
			Log.i("Acess", " response ="+response);
			
			return response;
			
		} finally{
			if(buffer != null)
				try {
					buffer.close();					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
		}
		
		
		
		
	}
	
	 public static String executeHttpGET(String url) throws Exception {
         BufferedReader bufferedReader = null;
         try {
            HttpClient Client = getHttpClient(); 
            HttpGet httpGet = new HttpGet(url);
            httpGet.setURI(new URI(url));
            HttpResponse httpResponse = Client.execute(httpGet); 
            bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            StringBuffer stringBuffer = new StringBuffer("");
            String line = "";
            String LS = System.getProperty("line.separator"); // quebra de linha
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line + LS);
            }
            bufferedReader.close();
            String resultado = stringBuffer.toString();
            return resultado;
         } finally {
             if (bufferedReader != null) {
                try {
                  bufferedReader.close();
                } catch (IOException e){
                      e.printStackTrace();
                   }
             }
           }
 }
	
	/*

	public boolean checarConectividade(){
		return false;// falta implementar
	}
	
	public boolean cadastrarUsuario(Usuario usuario){
		return false;// falta implementar
	}
	
	public Usuario consultarDadosUsuario(Usuario usuario){
	return null; //falta implementar
	}
	
	public boolean alterarDadosUsuario(Usuario antigo, Usuario novo){
		return false;  //falta implementar
	
	}
	
	public Personagem busca(Usuario usuario){
		return null; //falta implementar
	}
	
	public boolean adicionarPersonagem(Usuario usuario, Personagem personagem){
		return false;  //falta implementar
	}
	
	public boolean consultarDispNomePersonagem(String nome){
		return false; //falta implementar 
	}*/
}

